package cn.vihub.controller;
import cn.vihub.param.ShopMenuParam;
import cn.vihub.pojo.ShopMenu;
import cn.vihub.service.ShopMenuService;
import cn.vihub.utils.Result;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
* 菜单数据表(shop_menu)表控制层
*
* @author xxxxx
*/
@RestController
@RequestMapping("/shop-user/menu")
public class MenuController {
/**
* 服务对象
*/
    @Autowired
    private ShopMenuService shopMenuService;


    /**
     * @author: 秋晚
     * @description:查看所有菜单
     * @params: []
     * @return: java.lang.Object
     * @date: 2025/7/20 16:55
     */
    @GetMapping("tree")
    public Object tree(){
        return Result.success(shopMenuService.getTree());
    }


    /**
     * @author: 秋晚
     * @description:保存菜单
     * @params: [cn.vihub.param.ShopMenuParam]
     * @return: cn.vihub.utils.Result<java.lang.Boolean>
     * @date: 2025/7/20 16:55
     */
    @PostMapping("saveMenu")
    public Result<Boolean> saveMenu(@RequestBody ShopMenuParam shopMenuParam){
        if(shopMenuService.saveMenu(shopMenuParam)){
            return Result.success();
        }else{
            return Result.error();
        }
    }


    /**
     * @author: 秋晚
     * @description:删除菜单
     * @params: [java.lang.Integer]
     * @return: cn.vihub.utils.Result<java.lang.Boolean>
     * @date: 2025/7/20 16:55
     */
    @DeleteMapping("deleteMenuById/{id}")
    public Result<Boolean> deleteMenuById(@PathVariable(name = "id") Integer id){
        //判断菜单旗下是否有子菜单  若有子菜单则禁止删除
        shopMenuService.isChildrenByPid(id);
        if(shopMenuService.removeById(id)){
            //删除成功
            return Result.success();
        }else{
            return Result.error(500,"删除失败!");
        }
    }


    /**
     * @author: 秋晚
     * @description:根据userId查询菜单
     * @params: [java.lang.Integer]
     * @return: java.lang.Object
     * @date: 2025/7/20 17:02
     */
    @GetMapping("getTreeByUserId/{userId}")
    public Object getTreeByUserId(@PathVariable(name = "userId") Integer userId){
        return Result.success(shopMenuService.getTreeByUserId(userId));
    }


    /**
     * @author: 秋晚
     * @description:根据角色id查询菜单
     * @params: [java.lang.Integer]
     * @return: cn.vihub.utils.Result<java.util.List<cn.vihub.pojo.ShopMenu>>
     * @date: 2025/7/23 20:29
     */
    @PostMapping("getMenuById/{roleId}")
    public Result<List<ShopMenu>> getMenuById(@PathVariable(name = "roleId") Integer roleId){
        return Result.success(shopMenuService.getMenuById(roleId));
    }
}
